
package example

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/example"
    exampleReq "github.com/flipped-aurora/gin-vue-admin/server/model/example/request"
)

type StudentProfileService struct {}
// CreateStudentProfile 创建学生档案记录
// Author [yourname](https://github.com/yourname)
func (SPService *StudentProfileService) CreateStudentProfile(SP *example.StudentProfile) (err error) {
	err = global.GVA_DB.Create(SP).Error
	return err
}

// DeleteStudentProfile 删除学生档案记录
// Author [yourname](https://github.com/yourname)
func (SPService *StudentProfileService)DeleteStudentProfile(ID string) (err error) {
	err = global.GVA_DB.Delete(&example.StudentProfile{},"id = ?",ID).Error
	return err
}

// DeleteStudentProfileByIds 批量删除学生档案记录
// Author [yourname](https://github.com/yourname)
func (SPService *StudentProfileService)DeleteStudentProfileByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]example.StudentProfile{},"id in ?",IDs).Error
	return err
}

// UpdateStudentProfile 更新学生档案记录
// Author [yourname](https://github.com/yourname)
func (SPService *StudentProfileService)UpdateStudentProfile(SP example.StudentProfile) (err error) {
	err = global.GVA_DB.Model(&example.StudentProfile{}).Where("id = ?",SP.ID).Updates(&SP).Error
	return err
}

// GetStudentProfile 根据ID获取学生档案记录
// Author [yourname](https://github.com/yourname)
func (SPService *StudentProfileService)GetStudentProfile(ID string) (SP example.StudentProfile, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&SP).Error
	return
}
// GetStudentProfileInfoList 分页获取学生档案记录
// Author [yourname](https://github.com/yourname)
func (SPService *StudentProfileService)GetStudentProfileInfoList(info exampleReq.StudentProfileSearch) (list []example.StudentProfile, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&example.StudentProfile{})
    var SPs []example.StudentProfile
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
    if info.FullName != nil && *info.FullName != "" {
        db = db.Where("full_name LIKE ?","%"+*info.FullName+"%")
    }
    if info.Gender != nil && *info.Gender != "" {
        db = db.Where("gender = ?",*info.Gender)
    }
    if info.StudentID != nil && *info.StudentID != "" {
        db = db.Where("student_id = ?",*info.StudentID)
    }
    if info.Class != nil && *info.Class != "" {
        db = db.Where("class = ?",*info.Class)
    }
    if info.Email != nil && *info.Email != "" {
        db = db.Where("email = ?",*info.Email)
    }
    if info.Phone != nil && *info.Phone != "" {
        db = db.Where("phone = ?",*info.Phone)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }
        var OrderStr string
        orderMap := make(map[string]bool)
         	orderMap["full_name"] = true
         	orderMap["gender"] = true
         	orderMap["birth_date"] = true
         	orderMap["student_id"] = true
         	orderMap["class"] = true
         	orderMap["major"] = true
         	orderMap["email"] = true
         	orderMap["phone"] = true
         	orderMap["address"] = true
       if orderMap[info.Sort] {
          OrderStr = info.Sort
          if info.Order == "descending" {
             OrderStr = OrderStr + " desc"
          }
          db = db.Order(OrderStr)
       }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&SPs).Error
	return  SPs, total, err
}
func (SPService *StudentProfileService)GetStudentProfilePublic() {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
